package com.yunhe.rentcars.pro.controller.Client;

import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.commons.util.UUIDUtils;
import com.yunhe.rentcars.pro.entity.client.ClientCompany;
import com.yunhe.rentcars.pro.entity.client.ClientUser;
import com.yunhe.rentcars.pro.service.client.ClientUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by Administrator on 2017/11/23 0023.
 */
@Controller
@RequestMapping("/client")
public class ClientUserController {
    @Autowired
    private ClientUserService clientUserService;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"),
                true));
    }
    @RequestMapping
   public String list(){
       return "/client/clientUser/list";
   }
   @ResponseBody
   @RequestMapping("/listData")
   public Pageable findPage(Integer offset,Integer limit){
       Pageable<ClientUser> page = clientUserService.findPage(offset,limit);
       return page;
   }
    @RequestMapping("/add")
    public String save() {
        return "/client/clientUser/add";
    }


   @RequestMapping("/save")
   public String save(ClientUser clientUser, ClientCompany clienCompany, HttpServletRequest request){

       clientUserService.save(clientUser, clienCompany);
       HttpSession session = request.getSession();
       session.setAttribute("clientUser" , clientUser);
       return "rent/SelectionAddr";
   }


    @RequestMapping("/find")
    public ModelAndView edit(Long uId){
        ClientUser clientUser = clientUserService.find(uId);
        ModelAndView modelAndView=new ModelAndView("/client/clientUser/edit");
        modelAndView.addObject("clientUser",clientUser);
        return modelAndView;
    }
    @RequestMapping("/update")
    public String update(ClientUser clientUser){
        clientUserService.update(clientUser);
        return "/client/clientUser/list";
    }
    @RequestMapping("/del")
    @ResponseBody
    public JSONResponse del(String[] ids){
        clientUserService.delBatch(ids);
        return JSONResponse.success("删除成功");
    }
    @RequestMapping("/searchData")
    @ResponseBody
    public Pageable searchData(String memLevel, Date startTime, Date endTime){
        Pageable<ClientUser> pageable = clientUserService.findSearchList(memLevel, startTime, endTime);
        return pageable;
    }

}
